package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.ICustomerListService;
import com.mdd.admin.validate.CustomerListCreateValidate;
import com.mdd.admin.validate.CustomerListUpdateValidate;
import com.mdd.admin.validate.CustomerListSearchValidate;
import com.mdd.admin.vo.CustomerColListVo;
import com.mdd.admin.vo.CustomerListDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.CustomerList;
import com.mdd.common.mapper.CustomerListMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 客户列实现类
 * @author LikeAdmin
 */
@Service
public class CustomerListServiceImpl implements ICustomerListService {
        
    @Resource
    CustomerListMapper customerListMapper;

    /**
     * 客户列列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<CustomerListListedVo>
     */
    @Override
    public PageResult<CustomerColListVo> list(PageValidate pageValidate, CustomerListSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<CustomerList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        customerListMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:customerId@customer_id:int",
            "=:caseType@case_type:str",
            "=:customerCode@customer_code:str",
            "=:customerNameChinese@customer_name_chinese:str",
            "=:customerNameEnglish@customer_name_english:str",
            "=:customerWebsite@customer_website:str",
            "=:companyOfficials@company_officials:str",
            "=:industryInvolvedId@industry_involved_id:int",
            "=:industryInvolved@industry_involved:str",
            "=:businessCooperationId@business_cooperation_id:int",
            "=:businessCooperation@business_cooperation:str",
            "=:businessPersonnelId@business_personnel_id:int",
            "=:businessPersonnel@business_personnel:str",
            "=:businessAssistantId@business_assistant_id:int",
            "=:businessAssistant@business_assistant:str",
            "=:processPersonnelId@process_personnel_id:int",
            "=:processPersonnel@Process_personnel:str",
            "=:subdivisionId@subdivision_id:int",
            "=:subdivision:str",
            "=:contractedCustomerTime@contracted_customer_time:str",
            "=:make bargainStatus@make bargain_status:str",
            "=:basicsCreateTime@basics_create_time:str",
            "like:basicsCreateName@basics_create_name:str",
            "=:associatesId@associates_id:int",
            "=:associates:str",
            "=:caseSourceCompanyId@case_source_company_id:int",
            "=:caseSourceCompany@case_source_company:str",
            "=:agencyGradeId@agency_grade_id:int",
            "=:agencyGrade@agency_grade:str",
            "=:customerSizeId@customer_size_id:int",
            "=:customerSize@customer_size:str",
            "=:nonPrincipalAgentId@non_principal_agent_id:int",
            "=:nonPrincipalAgent@non_principal_agent:str",
            "=:uploadsUserId@uploads_user_id:int",
            "like:uploadsUserName@uploads_user_name:str",
            "=:uploadsTime@uploads_time:str",
            "=:clientIp@client_ip:str",
            "=:clientAccount@client_account:str",
            "=:remark:str",
            "=:contactInformation@contact_information:str",
            "like:nickname:str",
            "=:phone:str",
            "=:fax:str",
            "=:email:str",
            "=:postcode:str",
            "=:clientGradeId@client_grade_id:int",
            "=:clientGrade@client_grade:str",
            "=:reputationGradeId@reputation_grade_id:int",
            "=:reputationGrade@reputation_grade:str",
            "=:confidentialityGradeId@confidentiality_grade_id:int",
            "=:confidentialityGrade@confidentiality_grade:str",
            "=:customerSourceId@customer_source_id:int",
            "=:customerSource@customer_source:str",
            "=:customerTownshipId@customer_township_id:int",
            "=:customerTownship@customer_township:str",
            "=:customerGardenId@customer_garden_id:int",
            "=:customerGarden@customer_garden:str",
            "=:customerTypeId@customer_type_id:int",
            "=:customerType@customer_type:str",
            "=:internalSource@internal_source:str",
            "=:internalSourcePhone@internal_source_phone:str",
            "=:internalSourceEmail@internal_source_email:str",
            "=:externalCaseSource@external_case_source:str",
            "=:externalCasePhone@external_case_phone:str",
            "=:externalCaseEmail@external_case_email:str",
            "=:sourceTypeId@source_type_id:int",
            "=:sourceType@source_type:str",
            "=:billingAddress@billing_address:str",
            "=:paymentTitle@Payment_title:str",
            "=:identificationNumberTaxpayer@identification_number_taxpayer:str",
            "=:invoiceTitle@invoice_title:str",
            "=:depositBank@deposit_bank:str",
            "=:bankAccount@bank_account:str",
            "=:annualSalesRevenue@annual_sales_revenue:str",
            "=:generalTaxpayer@general_taxpayer:str",
            "=:agencyDiscountRate@agency_discount_rate:str",
            "like:accountName@account_name:str",
            "=:totalAssets@total_assets:str",
            "=:phoneInfo@phone_info:str",
            "=:nation:str",
            "=:province:str",
            "=:cityChinese@city_chinese:str",
            "=:administrativeDivision@administrative_division:str",
            "=:businessAddress@business_address:str",
            "=:businessPhone@business_phone:str",
            "=:addressChinese@address_chinese:str",
            "=:addressEnglish@address_english:str",
            "=:additionalAddress@additional_address:str",
            "=:patentQuantity@patent_quantity:str",
            "=:numberOfTrademarks@number_of_trademarks:str",
            "=:copyrightNumber@copyright_number:str",
            "=:evaluationBasis@evaluation_basis:str",
            "=:followUpTime@follow_up_time:str",
            "=:unfollowedId@unfollowed_id:str",
            "=:unfollowed:str",
            "=:updateUserId@update_user_id:int",
        });

        IPage<CustomerList> iPage = customerListMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<CustomerColListVo> list = new LinkedList<>();
        for(CustomerList item : iPage.getRecords()) {
            CustomerColListVo vo = new CustomerColListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 客户列详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return CustomerList
     */
    @Override
    public CustomerListDetailVo detail(Integer id) {
        CustomerList model = customerListMapper.selectOne(
                new QueryWrapper<CustomerList>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        CustomerListDetailVo vo = new CustomerListDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 客户列新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(CustomerListCreateValidate createValidate) {
        CustomerList model = new CustomerList();
        model.setCustomerId(createValidate.getCustomerId());
        model.setCaseType(createValidate.getCaseType());
        model.setCustomerCode(createValidate.getCustomerCode());
        model.setCustomerNameChinese(createValidate.getCustomerNameChinese());
        model.setCustomerNameEnglish(createValidate.getCustomerNameEnglish());
        model.setCustomerWebsite(createValidate.getCustomerWebsite());
        model.setCompanyOfficials(createValidate.getCompanyOfficials());
        model.setIndustryInvolvedId(createValidate.getIndustryInvolvedId());
        model.setIndustryInvolved(createValidate.getIndustryInvolved());
        model.setBusinessCooperationId(createValidate.getBusinessCooperationId());
        model.setBusinessCooperation(createValidate.getBusinessCooperation());
        model.setBusinessPersonnelId(createValidate.getBusinessPersonnelId());
        model.setBusinessPersonnel(createValidate.getBusinessPersonnel());
        model.setBusinessAssistantId(createValidate.getBusinessAssistantId());
        model.setBusinessAssistant(createValidate.getBusinessAssistant());
        model.setProcessPersonnelId(createValidate.getProcessPersonnelId());
        model.setProcessPersonnel(createValidate.getProcessPersonnel());
        model.setSubdivisionId(createValidate.getSubdivisionId());
        model.setSubdivision(createValidate.getSubdivision());
        model.setContractedCustomerTime(createValidate.getContractedCustomerTime());
        model.setMakebargainStatus(createValidate.getMakebargainStatus());
        model.setBasicsCreateTime(createValidate.getBasicsCreateTime());
        model.setBasicsCreateName(createValidate.getBasicsCreateName());
        model.setAssociatesId(createValidate.getAssociatesId());
        model.setAssociates(createValidate.getAssociates());
        model.setCaseSourceCompanyId(createValidate.getCaseSourceCompanyId());
        model.setCaseSourceCompany(createValidate.getCaseSourceCompany());
        model.setAgencyGradeId(createValidate.getAgencyGradeId());
        model.setAgencyGrade(createValidate.getAgencyGrade());
        model.setCustomerSizeId(createValidate.getCustomerSizeId());
        model.setCustomerSize(createValidate.getCustomerSize());
        model.setNonPrincipalAgentId(createValidate.getNonPrincipalAgentId());
        model.setNonPrincipalAgent(createValidate.getNonPrincipalAgent());
        model.setUploadsUserId(createValidate.getUploadsUserId());
        model.setUploadsUserName(createValidate.getUploadsUserName());
        model.setUploadsTime(createValidate.getUploadsTime());
        model.setClientIp(createValidate.getClientIp());
        model.setClientAccount(createValidate.getClientAccount());
        model.setRemark(createValidate.getRemark());
        model.setContactInformation(createValidate.getContactInformation());
        model.setNickname(createValidate.getNickname());
        model.setPhone(createValidate.getPhone());
        model.setFax(createValidate.getFax());
        model.setEmail(createValidate.getEmail());
        model.setPostcode(createValidate.getPostcode());
        model.setClientGradeId(createValidate.getClientGradeId());
        model.setClientGrade(createValidate.getClientGrade());
        model.setReputationGradeId(createValidate.getReputationGradeId());
        model.setReputationGrade(createValidate.getReputationGrade());
        model.setConfidentialityGradeId(createValidate.getConfidentialityGradeId());
        model.setConfidentialityGrade(createValidate.getConfidentialityGrade());
        model.setCustomerSourceId(createValidate.getCustomerSourceId());
        model.setCustomerSource(createValidate.getCustomerSource());
        model.setCustomerTownshipId(createValidate.getCustomerTownshipId());
        model.setCustomerTownship(createValidate.getCustomerTownship());
        model.setCustomerGardenId(createValidate.getCustomerGardenId());
        model.setCustomerGarden(createValidate.getCustomerGarden());
        model.setCustomerTypeId(createValidate.getCustomerTypeId());
        model.setCustomerType(createValidate.getCustomerType());
        model.setInternalSource(createValidate.getInternalSource());
        model.setInternalSourcePhone(createValidate.getInternalSourcePhone());
        model.setInternalSourceEmail(createValidate.getInternalSourceEmail());
        model.setExternalCaseSource(createValidate.getExternalCaseSource());
        model.setExternalCasePhone(createValidate.getExternalCasePhone());
        model.setExternalCaseEmail(createValidate.getExternalCaseEmail());
        model.setSourceTypeId(createValidate.getSourceTypeId());
        model.setSourceType(createValidate.getSourceType());
        model.setBillingAddress(createValidate.getBillingAddress());
        model.setPaymentTitle(createValidate.getPaymentTitle());
        model.setIdentificationNumberTaxpayer(createValidate.getIdentificationNumberTaxpayer());
        model.setInvoiceTitle(createValidate.getInvoiceTitle());
        model.setDepositBank(createValidate.getDepositBank());
        model.setBankAccount(createValidate.getBankAccount());
        model.setAnnualSalesRevenue(createValidate.getAnnualSalesRevenue());
        model.setGeneralTaxpayer(createValidate.getGeneralTaxpayer());
        model.setAgencyDiscountRate(createValidate.getAgencyDiscountRate());
        model.setAccountName(createValidate.getAccountName());
        model.setTotalAssets(createValidate.getTotalAssets());
        model.setPhoneInfo(createValidate.getPhoneInfo());
        model.setNation(createValidate.getNation());
        model.setProvince(createValidate.getProvince());
        model.setCityChinese(createValidate.getCityChinese());
        model.setAdministrativeDivision(createValidate.getAdministrativeDivision());
        model.setBusinessAddress(createValidate.getBusinessAddress());
        model.setBusinessPhone(createValidate.getBusinessPhone());
        model.setAddressChinese(createValidate.getAddressChinese());
        model.setAddressEnglish(createValidate.getAddressEnglish());
        model.setAdditionalAddress(createValidate.getAdditionalAddress());
        model.setPatentQuantity(createValidate.getPatentQuantity());
        model.setNumberOfTrademarks(createValidate.getNumberOfTrademarks());
        model.setCopyrightNumber(createValidate.getCopyrightNumber());
        model.setEvaluationBasis(createValidate.getEvaluationBasis());
        model.setFollowUpTime(createValidate.getFollowUpTime());
        model.setUnfollowedId(createValidate.getUnfollowedId());
        model.setUnfollowed(createValidate.getUnfollowed());
        model.setUpdateUserId(createValidate.getUpdateUserId());
        customerListMapper.insert(model);
    }

    /**
     * 客户列编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(CustomerListUpdateValidate updateValidate) {
        CustomerList model = customerListMapper.selectOne(
                new QueryWrapper<CustomerList>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setCustomerId(updateValidate.getCustomerId());
        model.setCaseType(updateValidate.getCaseType());
        model.setCustomerCode(updateValidate.getCustomerCode());
        model.setCustomerNameChinese(updateValidate.getCustomerNameChinese());
        model.setCustomerNameEnglish(updateValidate.getCustomerNameEnglish());
        model.setCustomerWebsite(updateValidate.getCustomerWebsite());
        model.setCompanyOfficials(updateValidate.getCompanyOfficials());
        model.setIndustryInvolvedId(updateValidate.getIndustryInvolvedId());
        model.setIndustryInvolved(updateValidate.getIndustryInvolved());
        model.setBusinessCooperationId(updateValidate.getBusinessCooperationId());
        model.setBusinessCooperation(updateValidate.getBusinessCooperation());
        model.setBusinessPersonnelId(updateValidate.getBusinessPersonnelId());
        model.setBusinessPersonnel(updateValidate.getBusinessPersonnel());
        model.setBusinessAssistantId(updateValidate.getBusinessAssistantId());
        model.setBusinessAssistant(updateValidate.getBusinessAssistant());
        model.setProcessPersonnelId(updateValidate.getProcessPersonnelId());
        model.setProcessPersonnel(updateValidate.getProcessPersonnel());
        model.setSubdivisionId(updateValidate.getSubdivisionId());
        model.setSubdivision(updateValidate.getSubdivision());
        model.setContractedCustomerTime(updateValidate.getContractedCustomerTime());
        model.setMakebargainStatus(updateValidate.getMakebargainStatus());
        model.setBasicsCreateTime(updateValidate.getBasicsCreateTime());
        model.setBasicsCreateName(updateValidate.getBasicsCreateName());
        model.setAssociatesId(updateValidate.getAssociatesId());
        model.setAssociates(updateValidate.getAssociates());
        model.setCaseSourceCompanyId(updateValidate.getCaseSourceCompanyId());
        model.setCaseSourceCompany(updateValidate.getCaseSourceCompany());
        model.setAgencyGradeId(updateValidate.getAgencyGradeId());
        model.setAgencyGrade(updateValidate.getAgencyGrade());
        model.setCustomerSizeId(updateValidate.getCustomerSizeId());
        model.setCustomerSize(updateValidate.getCustomerSize());
        model.setNonPrincipalAgentId(updateValidate.getNonPrincipalAgentId());
        model.setNonPrincipalAgent(updateValidate.getNonPrincipalAgent());
        model.setUploadsUserId(updateValidate.getUploadsUserId());
        model.setUploadsUserName(updateValidate.getUploadsUserName());
        model.setUploadsTime(updateValidate.getUploadsTime());
        model.setClientIp(updateValidate.getClientIp());
        model.setClientAccount(updateValidate.getClientAccount());
        model.setRemark(updateValidate.getRemark());
        model.setContactInformation(updateValidate.getContactInformation());
        model.setNickname(updateValidate.getNickname());
        model.setPhone(updateValidate.getPhone());
        model.setFax(updateValidate.getFax());
        model.setEmail(updateValidate.getEmail());
        model.setPostcode(updateValidate.getPostcode());
        model.setClientGradeId(updateValidate.getClientGradeId());
        model.setClientGrade(updateValidate.getClientGrade());
        model.setReputationGradeId(updateValidate.getReputationGradeId());
        model.setReputationGrade(updateValidate.getReputationGrade());
        model.setConfidentialityGradeId(updateValidate.getConfidentialityGradeId());
        model.setConfidentialityGrade(updateValidate.getConfidentialityGrade());
        model.setCustomerSourceId(updateValidate.getCustomerSourceId());
        model.setCustomerSource(updateValidate.getCustomerSource());
        model.setCustomerTownshipId(updateValidate.getCustomerTownshipId());
        model.setCustomerTownship(updateValidate.getCustomerTownship());
        model.setCustomerGardenId(updateValidate.getCustomerGardenId());
        model.setCustomerGarden(updateValidate.getCustomerGarden());
        model.setCustomerTypeId(updateValidate.getCustomerTypeId());
        model.setCustomerType(updateValidate.getCustomerType());
        model.setInternalSource(updateValidate.getInternalSource());
        model.setInternalSourcePhone(updateValidate.getInternalSourcePhone());
        model.setInternalSourceEmail(updateValidate.getInternalSourceEmail());
        model.setExternalCaseSource(updateValidate.getExternalCaseSource());
        model.setExternalCasePhone(updateValidate.getExternalCasePhone());
        model.setExternalCaseEmail(updateValidate.getExternalCaseEmail());
        model.setSourceTypeId(updateValidate.getSourceTypeId());
        model.setSourceType(updateValidate.getSourceType());
        model.setBillingAddress(updateValidate.getBillingAddress());
        model.setPaymentTitle(updateValidate.getPaymentTitle());
        model.setIdentificationNumberTaxpayer(updateValidate.getIdentificationNumberTaxpayer());
        model.setInvoiceTitle(updateValidate.getInvoiceTitle());
        model.setDepositBank(updateValidate.getDepositBank());
        model.setBankAccount(updateValidate.getBankAccount());
        model.setAnnualSalesRevenue(updateValidate.getAnnualSalesRevenue());
        model.setGeneralTaxpayer(updateValidate.getGeneralTaxpayer());
        model.setAgencyDiscountRate(updateValidate.getAgencyDiscountRate());
        model.setAccountName(updateValidate.getAccountName());
        model.setTotalAssets(updateValidate.getTotalAssets());
        model.setPhoneInfo(updateValidate.getPhoneInfo());
        model.setNation(updateValidate.getNation());
        model.setProvince(updateValidate.getProvince());
        model.setCityChinese(updateValidate.getCityChinese());
        model.setAdministrativeDivision(updateValidate.getAdministrativeDivision());
        model.setBusinessAddress(updateValidate.getBusinessAddress());
        model.setBusinessPhone(updateValidate.getBusinessPhone());
        model.setAddressChinese(updateValidate.getAddressChinese());
        model.setAddressEnglish(updateValidate.getAddressEnglish());
        model.setAdditionalAddress(updateValidate.getAdditionalAddress());
        model.setPatentQuantity(updateValidate.getPatentQuantity());
        model.setNumberOfTrademarks(updateValidate.getNumberOfTrademarks());
        model.setCopyrightNumber(updateValidate.getCopyrightNumber());
        model.setEvaluationBasis(updateValidate.getEvaluationBasis());
        model.setFollowUpTime(updateValidate.getFollowUpTime());
        model.setUnfollowedId(updateValidate.getUnfollowedId());
        model.setUnfollowed(updateValidate.getUnfollowed());
        model.setUpdateUserId(updateValidate.getUpdateUserId());
        customerListMapper.updateById(model);
    }

    /**
     * 客户列删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        CustomerList model = customerListMapper.selectOne(
                new QueryWrapper<CustomerList>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        customerListMapper.updateById(model);
    }

}
